package com.webinsky.folder.persist.dao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectKey;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.JdbcType;

import com.webinsky.folder.persist.entity.FolderBookIndex;

public interface FolderBookIndexMapper {
	@Insert({
		"insert into site_index (site_id, item_id, catalog_id, article_id, sort, del, create_at, update_at, language)",
		"values (#{siteId,jdbcType=INTEGER}, #{itemId,jdbcType=INTEGER}, #{catalogId,jdbcType=INTEGER}, #{articleId,jdbcType=INTEGER}, #{sort,jdbcType=INTEGER}, #{del,jdbcType=INTEGER}, now(), now(), #{language,jdbcType=VARCHAR})"
	})
	@SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
	int insert(FolderBookIndex record);
	    
	@Select({
	    "select",
	    "id, site_id, item_id, catalog_id, article_id, sort, del, create_at, update_at, language",
	    "from site_index"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="item_id", property="itemId", jdbcType=JdbcType.INTEGER),
	    @Result(column="catalog_id", property="catalogId", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
	})
	List<FolderBookIndex> selectAll();
	
	@Select({
	    "select",
	    "id, site_id, item_id, catalog_id, article_id, sort, del, create_at, update_at, language",
	    "from site_index",
	    "where id = #{id, jdbcType=INTEGER}"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="item_id", property="itemId", jdbcType=JdbcType.INTEGER),
	    @Result(column="catalog_id", property="catalogId", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
	})	
	FolderBookIndex selectById(@Param("id")int id);
	
	@Select({
		"select",
	    "id, site_id, item_id, catalog_id, article_id, sort, del, create_at, update_at, language",
	    "from site_index",
	    "where id = #{id, jdbcType=INTEGER} and del = #{del, jdbcType=INTEGER}"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="item_id", property="itemId", jdbcType=JdbcType.INTEGER),
	    @Result(column="catalog_id", property="catalogId", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
	})
	List<FolderBookIndex> selectByIdAndDel(@Param("id")int id, @Param("del")int del);
	
	@Select({
		"select",
	    "id, site_id, item_id, catalog_id, article_id, sort, del, create_at, update_at, language",
	    "from site_index",
	    "where article_id = #{articleId, jdbcType=INTEGER} and del = #{del, jdbcType=INTEGER}"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="item_id", property="itemId", jdbcType=JdbcType.INTEGER),
	    @Result(column="catalog_id", property="catalogId", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
	})
	List<FolderBookIndex> selectByArticleIdAndDel(@Param("articleId")int articleId, @Param("del")int del);
	
	@Select({
		"select",
	    "id, site_id, item_id, catalog_id, article_id, sort, del, create_at, update_at, language",
	    "from site_index",
	    "where item_id = #{itemId, jdbcType=INTEGER} and del = #{del, jdbcType=INTEGER}"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="item_id", property="itemId", jdbcType=JdbcType.INTEGER),
	    @Result(column="catalog_id", property="catalogId", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
	})
	List<FolderBookIndex> selectByItemIdAndDel(@Param("itemId")int itemId, @Param("del")int del);
	
	@Select({
		"select",
	    "id, site_id, item_id, catalog_id, article_id, sort, del, create_at, update_at, language",
	    "from site_index",
	    "where site_id = #{siteId, jdbcType=INTEGER} and del = #{del, jdbcType=INTEGER} and catalog_id <= 0"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="item_id", property="itemId", jdbcType=JdbcType.INTEGER),
	    @Result(column="catalog_id", property="catalogId", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
	})
	List<FolderBookIndex> selectItemByDel(@Param("siteId")int siteId, @Param("del")int del);
	
	@Select({
		"select",
	    "id, site_id, item_id, catalog_id, article_id, sort, del, create_at, update_at, language",
	    "from site_index",
	    "where site_id = #{siteId, jdbcType=INTEGER} and del = #{del, jdbcType=INTEGER} and item_id <= 0"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="item_id", property="itemId", jdbcType=JdbcType.INTEGER),
	    @Result(column="catalog_id", property="catalogId", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
	})
	List<FolderBookIndex> selectCatalogByDel(@Param("siteId")int siteId, @Param("del")int del);	
	
	
	@Select({
		"select",
	    "id, site_id, item_id, catalog_id, article_id, sort, del, create_at, update_at, language",
	    "from site_index",
	    "where catalog_id = #{catalogId, jdbcType=INTEGER} and del = #{del, jdbcType=INTEGER}"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="item_id", property="itemId", jdbcType=JdbcType.INTEGER),
	    @Result(column="catalog_id", property="catalogId", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
	})
	List<FolderBookIndex> selectByCatalogIdAndDel(@Param("catalogId")int catalogId, @Param("del")int del);
		
	@Select({
		"select",
	    "id, site_id, item_id, catalog_id, article_id, sort, del, create_at, update_at, language",
	    "from site_index",
	    "where site_id = #{siteId, jdbcType=INTEGER} and del = #{del, jdbcType=INTEGER} and language = #{lang, jdbcType=VARCHAR}"
	})
	@Results({
	    @Result(column="id", property="id", jdbcType=JdbcType.INTEGER),
	    @Result(column="site_id", property="siteId", jdbcType=JdbcType.INTEGER),
	    @Result(column="item_id", property="itemId", jdbcType=JdbcType.INTEGER),
	    @Result(column="catalog_id", property="catalogId", jdbcType=JdbcType.INTEGER),
	    @Result(column="article_id", property="articleId", jdbcType=JdbcType.INTEGER),
	    @Result(column="sort", property="sort", jdbcType=JdbcType.INTEGER),
	    @Result(column="del", property="del", jdbcType=JdbcType.INTEGER),
	    @Result(column="create_at", property="createAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="update_at", property="updateAt", jdbcType=JdbcType.TIMESTAMP),
	    @Result(column="language", property="language", jdbcType=JdbcType.VARCHAR),
	})
	List<FolderBookIndex> selectBySiteIdAndLang(@Param("siteId")int siteId, @Param("lang")String lang, @Param("del")int del);
	
	@Update({
	   "update site_index",
	   "set site_id = #{siteId,jdbcType=INTEGER},",
		   "item_id = #{itemId,jdbcType=INTEGER},",
		   "catalog_id = #{catalogId,jdbcType=INTEGER},",
		   "article_id = #{articleId,jdbcType=INTEGER},",
		   "sort = #{sort, jdbcType=INTEGER},",
		   "del = #{del,jdbcType=INTEGER},",
		   "create_at = #{createAt,jdbcType=TIMESTAMP},",
		   "update_at = #{updateAt,jdbcType=TIMESTAMP},",
		   "language = #{language,jdbcType=VARCHAR}",
	   "where Id = #{id,jdbcType=INTEGER}"
	   })
	int updateByPrimaryKey(FolderBookIndex record);	
}
